﻿using MediatR;
using Medical.Api.Write.Application.Command.PackagingSterilization;
using Medical.Domain.PackagingSterilization;
using Medical.ErrorCode;
using Medical.Interstructrue.Base;

namespace Medical.Api.Write.Application.Handler.PackagingSterilization
{
    /// <summary>
    /// 删除包装 sterilization
    /// </summary>
    public class DeletePackagingSterilizationHandler : IRequestHandler<DeletePackagingSterilizationCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<PackagingRegistration> _packRepository;
        private readonly ILogger<DeletePackagingSterilizationHandler> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="packRepository"></param>
        /// <param name="logger"></param>
        public DeletePackagingSterilizationHandler(IBaseRepository<PackagingRegistration> packRepository, ILogger<DeletePackagingSterilizationHandler> logger)
        {
            _packRepository = packRepository;
            _logger = logger;
        }
        /// <summary>
        /// 删除包装方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<int>> Handle(DeletePackagingSterilizationCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            var model = _packRepository.GetModel(request.Id);
            if (model == null)
            {
                res.Code = ApiEnums.Error;
                res.Msg = "包装 sterilization 不存在";
                res.Data = 0;
                return Task.FromResult(res);
            }
            model.IsDeleted = true;
            var r = _packRepository.Update(model);
            if (r > 0)
            {
                res.Code = ApiEnums.Success;
                res.Msg = "删除包装 sterilization 成功";
                res.Data = 0;
                _logger.LogInformation($"删除包装 sterilization 成功，Id: {request.Id}");
                return Task.FromResult(res);
            }
            else
            {
                res.Code = ApiEnums.Fail;
                res.Msg = "删除包装 sterilization 失败";
                res.Data = 0;
                _logger.LogError($"删除包装 sterilization 失败，Id: {request.Id}");
                return Task.FromResult(res);

            }
        }
    }
}
